package com.hengpeng.common.web.tags;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTagSupport;

/**
 * <pre>
 * 增加校验的标签
 * </pre>
 * @author linriqing
 * @version 1.0, 2006-6-16
 */
public class ValidateTag extends BodyTagSupport
{
	private static String event;

	private static String invoke;

	private static final long serialVersionUID = 3356762574464482141L;

	static
	{
		invoke = "checkValidate(this);";
		event = "onChange";
	}

	private String autoShowStar;

	private String dataType;

	private String dispName;

	private String maxL;

	private String maxValue;

	private String minValue;

	/**
	 * 构造函数
	 */
	public ValidateTag()
	{
		dispName = null;
		dataType = null;
		maxL = null;
		minValue = null;
		maxValue = null;
		autoShowStar = null;
	}

	public int doAfterBody() throws JspException
	{
		super.doAfterBody();
		BodyContent body = this.getBodyContent();
		String content = body.getString();
		body.clearBody();

		try
		{
			getPreviousOut().print(addProperties(content));
		}
		catch (IOException e)
		{
			throw new JspException(e);
		}
		return SKIP_BODY;
	}

	public int doStartTag() throws JspException
	{
		super.doStartTag();
		return EVAL_BODY_BUFFERED;
	}

	/**
	 * @return Returns the autoShowStar.
	 */
	public String getAutoShowStar()
	{
		return autoShowStar;
	}

	/**
	 * @return Returns the dataType.
	 */
	public String getDataType()
	{
		return dataType;
	}

	/**
	 * @return Returns the dispName.
	 */
	public String getDispName()
	{
		return dispName;
	}

	/**
	 * @return Returns the maxL.
	 */
	public String getMaxL()
	{
		return maxL;
	}

	/**
	 * @return Returns the maxValue.
	 */
	public String getMaxValue()
	{
		return maxValue;
	}

	/**
	 * @return Returns the minValue.
	 */
	public String getMinValue()
	{
		return minValue;
	}

	/**
	 * @param autoShowStar The autoShowStar to set.
	 */
	public void setAutoShowStar(String autoShowStar)
	{
		this.autoShowStar = autoShowStar;
	}

	/**
	 * @param dataType The dataType to set.
	 */
	public void setDataType(String dataType)
	{
		this.dataType = dataType;
	}

	/**
	 * @param dispName The dispName to set.
	 */
	public void setDispName(String dispName)
	{
		this.dispName = dispName;
	}

	/**
	 * @param maxL The maxL to set.
	 */
	public void setMaxL(String maxL)
	{
		this.maxL = maxL;
	}

	/**
	 * @param maxValue The maxValue to set.
	 */
	public void setMaxValue(String maxValue)
	{
		this.maxValue = maxValue;
	}

	/**
	 * @param minValue The minValue to set.
	 */
	public void setMinValue(String minValue)
	{
		this.minValue = minValue;
	}

	protected String addProperties(String content)
	{
		int beginTagIndex = content.indexOf("<");
		if (beginTagIndex == -1)
		{
			return content;
		}

		int endTagIndex = content.indexOf(">", beginTagIndex);
		if (endTagIndex == -1)
		{
			return content;
		}

		if (content.charAt(endTagIndex - 1) == '/')
		{
			endTagIndex--;
		}

		StringBuffer sb = new StringBuffer();
		if ((content.indexOf("onchange") > 0) || (content.indexOf("onChange") > 0))
		{
			String t = "onchange=\"";
			int changeevent = content.indexOf("onchange=\"");
			if (changeevent < 0)
			{
				changeevent = content.indexOf("onChange=\"");
			}
			int vcstart = changeevent + t.length();
			sb.append(content.substring(0, vcstart));
			sb.append(invoke);
			endTagIndex = content.indexOf(">");
			if (content.charAt(endTagIndex - 1) == '/')
			{
				endTagIndex--;
			}
			sb.append(content.substring(vcstart, endTagIndex));
			modifyDefin(sb);
			sb.append(content.substring(endTagIndex));

		}
		else
		{
			sb.append(content.substring(0, endTagIndex));
			resovleProperties(sb);
			sb.append(content.substring(endTagIndex));
		}

		return sb.toString();
	}

	protected void modifyChangeEvent(StringBuffer sb)
	{
		sb.append(" ");
		sb.append(event);
		sb.append("='");
		sb.append(invoke);
		sb.append("'");
	}

	protected void modifyDefin(StringBuffer sb)
	{

		if (dispName != null)
		{
			sb.append(" dispName='");
			sb.append(dispName);
			sb.append("'");
		}

		if (dataType != null)
		{
			sb.append(" dataType='");
			sb.append(dataType);
			sb.append("'");
		}

		if (maxL != null)
		{
			sb.append(" maxL='");
			sb.append(maxL);
			sb.append("'");
		}

		if (maxValue != null)
		{
			sb.append(" maxValue='");
			sb.append(maxValue);
			sb.append("'");
		}

		if (minValue != null)
		{
			sb.append(" minValue='");
			sb.append(minValue);
			sb.append("'");
		}
		if (autoShowStar != null)
		{
			sb.append(" autoShowStar='");
			sb.append(autoShowStar);
			sb.append("'");
		}
	}

	protected void resovleProperties(StringBuffer sb)
	{
		modifyChangeEvent(sb);
		modifyDefin(sb);

	}
}